package com.example.contentcenter.controller;

import com.example.contentcenter.dao.content.ShareMapper;
import com.example.contentcenter.domain.entity.content.Share;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: TestController
 * @Author: lerry_li
 * @CreateDate: 2021/05/30
 * @Description
 */
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {

    private final ShareMapper shareMapper;

    private final DiscoveryClient discoveryClient;

    @GetMapping("/test")
    public List<Share> testInsert() {
        //1.插入
        Share share = new Share();
        share.setCreateTime(new Date());
        share.setUpdateTime(new Date());
        share.setTitle("xxx");
        share.setCover("xxx");
        share.setAuthor("lerry");
        share.setBuyCount(1);
        this.shareMapper.insertSelective(share);
        //2.查询
        List<Share> shareList = this.shareMapper.selectAll();

        return shareList;
    }

    /**
     * 测试：服务发现，证明内容中心能够找到用户中心
     * @return 用户中心所有实例的信息
     */
    @GetMapping("test2")
    public List<ServiceInstance> getInstances() {
        //查询指定服务的所有实例的信息
        return this.discoveryClient.getInstances("user-center");
    }

}
